using Sinacor.UI.Bovespa.Contabil.Communication;
using Sinacor.Infra.UI.Common;
using System.Collections.Generic;
using Sinacor.UI.Bovespa.Contabil.Communication.Bovespa.Contabil.DespesaNota;
using Sinacor.Infra.UI.Security.Services;
using System;

namespace Sinacor.UI.Bovespa.Contabil.Communication
{
    public class DespesaNota : BaseCommunication
    {
        public DespesaNota(SessionInformation session) : base(session) { }

        public void AlterarDespesaNota(Model.DespesaNota model)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.AlterarDespesaNota(_toDataContract.ToDespesaNotaRequest(model));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public void AlterarDespesasNota(ModelCollection<Model.DespesaNota> modelList)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.AlterarDespesasNota(_toDataContract.ToDespesaNotaRequest(modelList));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public ModelCollection<Model.DespesaNota> ConsultarDespesasNota(Model.Parameter.FiltroDespesaNota parameter)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    Translator.DataContractToModel.DespesaNota _toModel = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.DataContractToModel.DespesaNota();
                    return _toModel.ToDespesaNota(servico.ConsultarDespesasNota(_toDataContract.ToFiltroDespesaNotaRequest(parameter)));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
                return null; 
            }
        }

        public void ExcluirDespesaNota(Model.Parameter.FiltroDespesaNotaChave parameter)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.ExcluirDespesaNota(_toDataContract.ToFiltroDespesaNotaChaveRequest(parameter));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public void ExcluirDespesasNota(ModelCollection<Model.Parameter.FiltroDespesaNotaChave> parameterList)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.ExcluirDespesasNota(_toDataContract.ToFiltroDespesaNotaChaveRequest(parameterList));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public void ExecutarTransacoesDespesaNota(ModelCollection<Model.DespesaNota> inclusaoList, ModelCollection<Model.Parameter.FiltroDespesaNotaChave> exclusaoList, ModelCollection<Model.DespesaNota> alteracaoList)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.ExecutarTransacoesDespesaNota(_toDataContract.ToFiltroDespesaNotaChaveRequest(exclusaoList),_toDataContract.ToDespesaNotaRequest(inclusaoList), _toDataContract.ToDespesaNotaRequest(alteracaoList));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public void IncluirAjustarDespesaNota(Model.DespesaNota model)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.IncluirAjustarDespesaNota(_toDataContract.ToDespesaNotaRequest(model));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

         public void IncluirAjustarDespesasNota(ModelCollection<Model.DespesaNota> modelList)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.IncluirAjustarDespesasNota(_toDataContract.ToDespesaNotaRequest(modelList));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public void IncluirDespesaNota(Model.DespesaNota model)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.IncluirDespesaNota(_toDataContract.ToDespesaNotaRequest(model));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }

        }

        public void IncluirDespesasNota(ModelCollection<Model.DespesaNota> modelList)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    servico.IncluirDespesasNota(_toDataContract.ToDespesaNotaRequest(modelList));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        public Model.DespesaNota ObterDespesaNota(Model.Parameter.FiltroDespesaNota parameter)
        {

            Model.Parameter.FiltroDespesaNotaChave _filtro = new Sinacor.UI.Bovespa.Contabil.Model.Parameter.FiltroDespesaNotaChave();
            _filtro.Codigo = parameter.Codigo;
            _filtro.PregaoInicial = parameter.PregaoInicial.Value ; 

            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    Translator.DataContractToModel.DespesaNota _toModel = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.DataContractToModel.DespesaNota();
                    return _toModel.ToDespesaNota(servico.ObterDespesaNota(_toDataContract.ToFiltroDespesaNotaChaveRequest(_filtro)));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
                return null;
            }
        }

        public Model.Conflito VerificarConflitoPeriodo(Model.DespesaNota request)
        {
            IDespesaNota servico = null;

            try
            {
                using (ServiceInstance<IDespesaNota> service = new ServiceInstance<IDespesaNota>())
                {
                    servico = service.TryGetInstance(this.Session);
                    Translator.ModelToDataContract.DespesaNota _toDataContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.DespesaNota();
                    Translator.DataContractToModel.DespesaNota _toModel = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.DataContractToModel.DespesaNota();
                    return _toModel.ToConflito(servico.VerificarConflitoPeriodo(_toDataContract.ToDespesaNotaRequest(request)));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
                return null;
            }
        }
    }
}